<template>
  <!-- 添加或修改公告 -->
  <div class="app-container-add">
    <!-- 功能按钮 -->
    <el-row :gutter="10" class="button-wrap" :style="{width:mainContentWidth + 'px','left':'80px'}">
      <el-col :span="1.5">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-document-checked" size="middle" @click="submitForm">保存</el-button>
      </el-col>
    </el-row>
    <!-- form表单 -->
    <div ref="mainCont" class="main-content">
      <el-card ref="mainContent" class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>{{ title }}</span>
        </div>
        <div class="main-content-body">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="公告标题" prop="noticeTitle">
                  <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="公告内容" prop="noticeContent">
              <tinymce v-model="form.noticeContent" />
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入富文本
import tinymce from '@/components/Tinymce'
import { addNotice, updateNotice, getNoticeByNoticeId } from '@/api/PersonalOffice/notice'
export default {
  name: 'AddDialog',
  components: {
    tinymce
  },
  props: {
  },
  data() {
    return {
      mainContentWidth: '',
      // 添加或修改对话框标题
      title: '',
      form: {
        noticeTitle: '',
        noticeType: '',
        status: '',
        noticeContent: ''
      },
      // 修改或添加对话框表单验证提示
      rules: {
        noticeTitle: [
          { required: true, message: '公告标题不能为空', trigger: 'blur' }
        ],
        noticeType: [
          { required: true, message: '公告类型不能为空', trigger: 'blur' }
        ]
      },
      typeOptions: []
    }
  },
  mounted() {
    /** 获取主题内容宽度 start*/
    setTimeout(() => {
      this.mainContentWidth = this.$refs.mainContent.$el.offsetWidth
    }, 200)
    const that = this
    window.onresize = () => {
      return (() => {
        that.mainContentWidth = that.$refs.mainContent.$el.offsetWidth
      })()
    }
    /** 获取主题内容宽度 end*/

    this.title = this.$route.query.title
    this.form.noticeId = this.$route.query.noticeId
    if (this.form.noticeId) {
      this.getDetailById(this.form.noticeId)
    }
    this.getDicts('sys_notice_type').then(response => {
      this.typeOptions = response.data.rows
    })
  },
  methods: {
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    // 清空from表单
    emptFrom() {
      this.form = {
        noticeId: '',
        noticeTitle: '',
        noticeType: '',
        status: '',
        noticeContent: ''
      }
    },
    // 获取详情
    async getDetailById(id) {
      const { data } = await getNoticeByNoticeId(id)
      this.form = { ...data }
    },
    // 添加或修改对话框确定按钮
    submitForm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          if (this.form.noticeId) {
            await updateNotice(this.form)
            this.msgSuccess('修改成功')
          } else {
            await addNotice(this.form)
            this.msgSuccess('新增成功')
          }
          this.emptFrom()
          this.$router.push({
            path: '/PersonalOffice/message'
          })
        }
      })
    }
  }
}
</script>

